//新建报表
function addReport(e) {
    e = e || window.event;
    layer.open({
        move: false,
        type: 2,
        title: '新建报表',
        area: ['545px', '200px'],
        content: 'j_report_add.html',
        btn: ['确定','取消'],
        btnAlign: 'c',
        yes: function (index, layero) {
        	var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var form= openWin.submitForm();
            if(!form){
                layer.close(index);
            }
        }, cancel: function (index) {
            layer.close(index);
        }
    });
    e.stopPropagation();
}
function classifyOpt() {
    $("#treeTable tr").hover(function(){
        $(this).find('.rt').removeClass('hidden');
    },function(){
        $(this).find('.rt').addClass('hidden');
    });
}
//新建分类弹出框
function addClassify(e) {
    e = e || window.event;
    layer.open({
        move: false,
        type: 2,
        title: '新建分类',
        area: ['500px', '200px'],
        content: 'j_report_add_classify.html',
        btn: ['确定','取消'],
        btnAlign: 'c',
        yes: function (index, layero) {
        	var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var form= openWin.submitForm();
            if(!form){
                layer.close(index);
            }
        }, cancel: function (index) {
            layer.close(index);
        }
    });
    e.stopPropagation();
}
//编辑分类弹出框
function editClassify(e) {
    e = e || window.event;
    layer.open({
        move: false,
        type: 2,
        title: '编辑分类',
        area: ['500px', '200px'],
        content: 'j_report_add_classify.html',
        btn: ['确定','取消'],
        btnAlign: 'c',
        yes: function (index, layero) {
            var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var form= openWin.submitForm();
            if(!form){
                layer.close(index);
            }
        }, cancel: function (index) {
            layer.close(index);
        }
    });
    e.stopPropagation();
}
//删除分类弹出框
function delClassify(e) {
    e = e || window.event;
    layer.confirm('确定要删除分类吗？',
        {icon: 3, title: '提示', area: '300px', btnAlign: 'c'}, function (index) {
            layer.close(index);
        });
    e.stopPropagation();
}
$(function () {
    $('#reportCode').focus();
    //重置表单
    $("#searchReset").on("click",function(){
        $('#reportCode').val('');
        $('#reportName').val('');
        $('#reportTable').bootstrapTable('refresh');
    });
    //渲染表格
    var columns = reportList.columnsInit();
    var table = reportList.gridInit(columns);
    var datas = reportList.dataInit();
    $("#reportTable").bootstrapTable("append",datas);
    //点击删除按钮信息弹出框
    $("#del-btn").click(function(){
        var len = $("#reportTable input:checked").length;
        if (len == 0) {
            //未选中数据提示框
            layer.msg('请选择数据',  {time:1000});
        } else {
            layer.confirm('确定要删除这条数据吗？',
                {icon: 3, title: '提示', area: '300px', btnAlign: 'c'}, function (index) {
                    layer.close(index);
                });
        }

    });
    //左侧树列表
    //tree
    var DataSource = [{
        "DataSourceID": 1,
        "reportName": "分类1",
        "expanded": "true",
        children: [{
            "DataSourceID": 2,
            "reportName": "分类11",
            "expanded": "true",
            children: [{
                "DataSourceID": 3,
                "reportName": "分类111",
                "expanded": "true",
            },{
                "DataSourceID": 4,
                "reportName": "分类112"
            }]
        },{
            "DataSourceID": 5,
            "reportName": "分类12"
        }]
    },{
        "DataSourceID": 6,
        "reportName": "分类2",
        "expanded": "true",
        children: [{
            "DataSourceID": 7,
            "reportName": "分类21"
        },{
            "DataSourceID": 8,
            "reportName": "分类22"
        }]
    }];
    var source ={
        dataType: "json",
        dataFields: [{
            name: "reportName",
            type: "string",
        },{
            name: 'children',
            type: 'array'
        },{
            name: 'expanded',
            type: 'bool'
        }],
        hierarchy: {
            root: 'children'
        },
        id: 'DataSourceID',
        localData: DataSource
    };
    var dataAdapter = new $.jqx.dataAdapter(source);
    var iHeight=$('#rightCont').height()-30;
    // create Tree Grid
    $("#treeTable").jqxTreeGrid({
        source: dataAdapter,
        width: '100%',
        altRows: true,
        selectionMode: "singleRow",
        height: iHeight,
        theme: 'bootstrap',
        showToolbar: false,
        enableHover: true,
        columns: [{
            text: "分类",
            align: "center",
            dataField: "reportName",
            cellsRenderer:function (row, column, value, rowData) {
                var container = '<span style="width: 100%; height: 100%;">';
                var data = '<span class="tree-item-text" title=" '+value+' ">' +value+ '</span>'
                var option = '<span class="rt tree-item-opt hidden">' +
                    '<a href="javascript:void(0)"><i class="glyphicon glyphicon-plus" onclick="addClassify(event)"></i></a>' +
                    '<a href="javascript:void(0)"><i class="glyphicon glyphicon-pencil" onclick="editClassify(event)"></i></a>' +
                    '<a href="javascript:void(0)"><i class="glyphicon glyphicon-trash" onclick="delClassify(event)"></i></a></span>';
                container += data;
                container += option;
                container += "</span>";
                return container;
            },
        }]
    });
    classifyOpt();
    //收起子菜单鼠标悬浮操作
    $("#treeTable").on('rowCollapse', function (event) {
        classifyOpt();
    });
    //展开子菜单鼠标悬浮操作
    $("#treeTable").on('rowExpand', function (event) {
        classifyOpt();
    });
    //点击左边tree,右侧table refresh
    $("#treeTable").on('rowSelect', function (event) {
        var row = event.args.row;
        $("#reportTable").bootstrapTable('refresh');
    });
})
;(function(undefined) {
    "use strict"
    var _global;

    function result(args,fn){
        var argsArr = Array.prototype.slice.call(args);
        if(argsArr.length > 0){
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }
    var reportList = {
        columnsInit:function(){
            return  [{
                field: 'id',
                title: '序号',
                width:'5%',
                align:'center',
                valign: 'middle',
                formatter: function (value, row, index) {
                    return index + 1;
                }
            }, {
                field: 'reportCode',
                valign: 'middle',
                title: '报表编码',
                sortable : true
            }, {
                field: 'reportName',
                valign: 'middle',
                title: '报表名称'
            },{
                checkbox: true
            },{
                field: 'edit',
                title: '编辑',
                width: 100,
                align: 'center',
                formatter: function (value, row, index) {
                    return [
                        '<li><a href="javascript:void(0)" title="编辑" class="opt-icon" onclick="addReport(event);"><i class="glyphicon glyphicon-pencil"></i></a></li>',
                    ].join('');
                }

            }]
        },
        gridInit:function(columns){
            return $("#reportTable").bootstrapTable({
                url:'',
                columns: columns,
                striped: 'true',
                classes: 'table table-hover qxttable',
                pagination: 'true',
                pageNumber: 1,
                pageSize: 10,
                pageList: [
                    10, 30, 50, 100, 200
                ]
            })
        },
        dataInit:function(){
            return  [{
                reportCode: '154548',
                reportName: 'ELE-JFPG-A1'
            },{
                reportCode: '154548',
                reportName: 'ELE-JFPG-A1'
            },{
                reportCode: '154548',
                reportName: 'ELE-JFPG-A1'
            },{
                reportCode: '154548',
                reportName: 'ELE-JFPG-A1'
            },{
                reportCode: '154548',
                reportName: 'ELE-JFPG-A1'
            },{
                reportCode: '154548',
                reportName: 'ELE-JFPG-A1'
            },{
                reportCode: '154548',
                reportName: 'ELE-JFPG-A1'
            },{
                reportCode: '154548',
                reportName: 'ELE-JFPG-A1'
            },{
                reportCode: '154548',
                reportName: 'ELE-JFPG-A1'
            },{
                reportCode: '154548',
                reportName: 'ELE-JFPG-A1'
            },{
                reportCode: '154548',
                reportName: 'ELE-JFPG-A1'
            }]
        }
    }

    // 将插件对象暴露给全局对象
    _global = (function(){ return this || (0, eval)('this'); }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = reportList;
    } else if (typeof define === "function" && define.amd) {
        define(function(){return reportList;});
    } else {
        !('plugin' in _global) && (_global.reportList = reportList);
    }
}());